'use strict';
const Controller = require('egg').Controller;

class UserController extends Controller {
    // 用户登录
    async userlogin() {
        const { ctx } = this;
        let res = { code: 1, msg: '登录成功' };

        // 接收提交上来的数据
        const data = ctx.request.body;
        // console.log(data);
        // 去数据库拿数据对比
        const result = await this.app.mysql.get('users', { tel: data.tel });
        // console.log(result);
        if (!result) {
            res = { code: -1, msg: '手机号不存在' };
        } else if (result.passwd != data.passwd) {
            res = { code: 0, msg: '密码错误' };
        } else {
            // 登录成功后保存session信息
            ctx.session.uid = result.uid;
            ctx.session.tel = result.tel;
            ctx.session.username = result.username;
            ctx.session.avater = result.avater;
            ctx.session.score = result.score;
        }
        res.uid = result.uid;
        ctx.body = res;
    }

    // 用户注册
    async reg() {
        const { ctx } = this;
        let res = { code: 1, msg: '注册成功' };
        // 1 接收客户端提交上来的post数据
        const data = ctx.request.body;
        // console.log(data);

        // 2 验证码不区分大小写
        if (data.code.toLowerCase() != ctx.session.code.toLowerCase()) {
            res = { code: -1, msg: '验证码错误' };
            ctx.body = res;
            return;
        }
        // 3 判空操作
        if (!data.tel || !data.passwd) {
            res = {
                code: 0,
                msg: '手机号密码不能为空',
            }
            ctx.body = res;
            return;
        }

        // 4 账号判断
        const sql = `SELECT users.tel from users`;
        const rel = await this.app.mysql.query(sql);
        rel.forEach(element => {
            if (element.tel === data.tel) {
                res = { code: 2, msg: '账号已存在' };
            }
            ctx.body = res;
            return;
        });


        // 5 将数据存入数据库
        const result = await this.app.mysql.insert('users', {
            tel: data.tel,
            passwd: data.passwd
        });
        // console.log(result);
        if (!result) {
            res = { code: -2, msg: '注册失败' };
        } else {
            //注册成功干什么:SESSION
            ctx.session.uid = result.uid;
            ctx.session.tel = result.tel;
        }
        res.uid = result.uid
        ctx.body = res;
    }

    // 用户重置密码
    async reset() {
        const { ctx } = this;
        let res = { code: 1, msg: '重置密码成功' };
        // 1 接收客户端提交上来的post数据
        const data = ctx.request.body;
        console.log(data);

        // 2 验证码不区分大小写
        if (data.code.toLowerCase() != ctx.session.code.toLowerCase()) {
            res = { code: -1, msg: '验证码错误' };
            ctx.body = res;
            return;
        }
        // 3 判空操作
        if (!data.tel || !data.passwd) {
            res = {
                code: 0,
                msg: '手机号密码不能为空',
            }
            ctx.body = res;
            return;
        }

        // 4 将数据存入数据库
        // UPDATE `posts` SET `name` = 'fengmk2', `modifiedAt` = NOW() WHERE custom_id = 456 ;
        const sql = `UPDATE users SET users.passwd = ${data.passwd} WHERE users.tel = ${data.tel}`;
        const result = await this.app.mysql.query(sql);
        console.log(result);
        if (!result) {
            res = { code: -2, msg: '重置密码失败' };
        } else {
            //注册成功干什么:SESSION
            ctx.session.uid = result.uid;
            ctx.session.tel = result.tel;
            res.regtel = data.tel;
        }
        ctx.body = res;
    }

    // 获取用户信息
    async user() {
        const { ctx } = this;

        ctx.body = {
            code: 1,
            userinfo: {
                username: ctx.session.username,
                avater: ctx.session.avater,
            },
        };
    }

    // 获取积分
    async setScore() {
        const { ctx } = this;
        let res = { code: 1, msg: '获取成功' };
        //接收客户端提交上来的POST数据
        const data = ctx.request.query;
        // console.log('获取积分', data);
        const result = await this.app.mysql.get('users', { uid: data.uid });
        // console.log(result);
        res.score = result.score;
        ctx.body = res;
    }

    // 积分增加
    async updateScore() {
        const { ctx } = this;
        let res = {};
        //接收客户端提交上来的POST数据
        const data = ctx.request.body;
        // console.log('sss', data);
        //数据处理
        const row = {
            score: data.score,
            s_status: 1
        };
        const options = {
            where: {
                uid: ctx.session.uid
            }
        };
        await this.app.mysql.update('users', row, options);
        const relsut = await this.app.mysql.get('users', { uid: ctx.session.uid });
        console.log(relsut);
        if (relsut.s_status === 1) {
            res = { code: 1, msg: '已签到' };
            res.score = relsut.score;
        }
        ctx.body = res;
    }

}

module.exports = UserController;